//package com.weseal.utils;
//
//import com.weseal.query.MasterInfoQuery;
//import com.weseal.response.CommonResult;
//import com.weseal.service.FunctionInfoService;
//import com.weseal.service.MasterInfoService;
//import com.weseal.service.RoleInfoService;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authc.credential.CredentialsMatcher;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.cache.Cache;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.SimplePrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.Collection;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
///**
// * @author weseal
// * @title
// * @description
// * @usage
// * @copyright Copyright 2014  rrmj Corporation. All rights reserved.
// * @company 上海众多美网络科技有限公司
// * @create 2017/10/30
// */
//public class MasterRealm extends AuthorizingRealm {
//
//
//	@Autowired
//	private MasterInfoService masterInfoService;
//
//	@Autowired
//	private FunctionInfoService functionInfoService;
//
//	@Autowired
//	private RoleInfoService roleInfoService;
//
//	@Override
//	public boolean supports(AuthenticationToken token) {
//		return token instanceof MasterUserPasswordToken;
//	}
//
//
//	@Override
//	public String getName() {
//		return "masterRealm";
//	}
//
//	@Override
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//		MasterInfoVo masterInfoVo = (MasterInfoVo) principalCollection.fromRealm(getName()).iterator().next();
//		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		Set<FunctionInfoVo> functionInfoVoSet = functionInfoService.queryByMasterId(masterInfoVo.getId());
//		List<RoleInfoVo> roleInfoVos = roleInfoService.queryByMasterId(masterInfoVo.getId());
//		Set<String> roleStr = new HashSet<String>();
//		for (RoleInfoVo vo : roleInfoVos) {
//			roleStr.add(vo.getRoleName());
//		}
//		info.setRoles(roleStr);
//		Set<String> functionInfoStr = new HashSet<String>();
//		fillFunctionInfoStr(functionInfoStr, functionInfoVoSet);
//		info.setStringPermissions(functionInfoStr);
//		return info;
//	}
//
//	private void fillFunctionInfoStr(Set<String> functionInfoStr, Collection<FunctionInfoVo> functionInfoVoSet) {
//		for (FunctionInfoVo vo : functionInfoVoSet) {
//			functionInfoStr.add(vo.getFunctionIdentity());
//			if (CollectionUtils.isNotEmpty(vo.getSubFunctionInfoVo())) {
//				fillFunctionInfoStr(functionInfoStr, vo.getSubFunctionInfoVo());
//			}
//		}
//	}
//
//
//
//	@Override
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//		MasterUserPasswordToken token = (MasterUserPasswordToken) authenticationToken;
//		MasterInfoQuery query = token.getMasterInfoQuery();
//			CommonResult<MasterInfoVo> commonResult = masterInfoService.login(query);
//		MasterInfoVo masterInfoVo = commonResult.getData();
//		return new SimpleAuthenticationInfo(masterInfoVo, "", getName());
//	}
//
//
//	@Override
//	public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
//		super.clearCachedAuthorizationInfo(principals);
//	}
//
//	@Override
//	public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
//		super.clearCachedAuthenticationInfo(principals);
//	}
//
//	@Override
//	public void clearCache(PrincipalCollection principals) {
//		super.clearCache(principals);
//	}
//
//
//	public void clearAllCachedAuthenticationInfo() {
//		getAuthenticationCache().clear();
//	}
//
//	/**
//	 * 更新用户授权信息缓存.
//	 */
//	public void clearCachedAuthorizationInfo(String principal) {
//		SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
//		clearCachedAuthorizationInfo(principals);
//	}
//
//	/**
//	 * 清除所有用户授权信息缓存.
//	 */
//	public void clearAllCachedAuthorizationInfo() {
//		Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
//		if (cache != null) {
//			for (Object key : cache.keys()) {
//				cache.remove(key);
//			}
//		}
//	}
//
//
//	public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
//		super.setCredentialsMatcher(credentialsMatcher);
//	}
//}
